<template>
  <el-dialog
    class="custom-dialog"
    v-model="dialogVisible"
    title="选择巡检人员"
    width="1200px"
    @close="handleClose"
  >
    <div class="custom-table-list-wrapper">
      <!-- <div class="search" :class="{ active: toggleSearchStatus }">
          <div class="search-left" ref="searchFromRef">
            <el-form class="search-form" inline>
              <el-form-item label="承租类型">
                <el-select
                  class="search-select"
                  v-model="searchParams.type"
                  placeholder="请选择"
                  clearable
                >
                  <el-option
                    v-for="(item, index) of dict['tenantType']"
                    :key="index"
                    :label="item.name"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="关键词">
                <el-input
                  class="search-input"
                  v-model="searchParams.name"
                  placeholder="请输入产权证号"
                  :suffix-icon="Search"
                ></el-input>
              </el-form-item>
            </el-form>
            <a
              class="search-left-toggle"
              v-if="showToggle"
              @click="handleToggleSearch"
            >
              <svg-icon :name="toggleSearchStatus ? 'up' : 'down'" />
              {{ toggleSearchStatus ? '收起' : '展开' }}
            </a>
          </div>
          <div class="search-right">
            <el-button type="primary" @click="initTableListAndSelect(1)"
              ><svg-icon name="search" size="14px" />搜索</el-button
            >
            <el-button @click="handleReset()"
              ><svg-icon name="reset" size="17px" />重置</el-button
            >
          </div>
        </div> -->
      <div class="content">
        <div class="content-table">
          <el-table
            class="custom-table"
            ref="tableRef"
            :data="tableData"
            @select="handleSelectOne"
            @select-all="handleSelectAll"
          >
            <el-table-column type="selection" v-if="isShowOperate" />
            <el-table-column type="index" label="序号" width="60px" />
            <el-table-column prop="userName" label="真实姓名" />
            <el-table-column prop="userPhone" label="联系方式" />
            <el-table-column prop="areaName" label="所属园区" />
            <!-- <el-table-column prop="userPhone" label="分类" width="80">
              <template #default="scope">
                {{ getDictValue('user_type', scope.row.type) }}
              </template>
            </el-table-column> -->
            <!-- <el-table-column prop="deptName" label="单位">
              <template #default="scope">
                {{
                  scope.row.unitName ||
                  scope.row.unitContent ||
                  scope.row.oaunit
                }}
              </template>
            </el-table-column> -->
            <!-- <el-table-column prop="deptName" label="部门" /> -->
            <el-table-column prop="userPhone" label="岗位">
              <template #default="scope">
                {{ getDictValue('inspector_post_type', scope.row.patrolPost) }}
              </template>
            </el-table-column>
          </el-table>
        </div>
        <!-- <div class="content-page">
            <el-pagination
              class="custom-pagination"
              :current-page="paginationParams.pageIndex"
              :page-size="paginationParams.pageSize"
              background
              :page-sizes="[10, 20, 30, 50]"
              layout="total, prev, pager, next, sizes, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="initTableListAndSelect"
            />
          </div> -->
      </div>
    </div>
    <template #footer v-if="isShowOperate">
      <el-button @click="handleClose(false)">取消</el-button>
      <el-button type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-dialog>
</template>
<script setup>
import { ref, onMounted, watch, nextTick } from 'vue'
import { useTableList } from '@/hooks/useTableList'
import { useDict } from '@/hooks/useDict'
import { getUserList } from '@/api/manager/systemManage/user.js'
import { ElMessage } from 'element-plus'
const { dict, getDictValue, getDictStyle } = useDict()
const searchParams = ref({})
const {
  Search,
  toggleSearchStatus,
  showToggle,
  handleToggleSearch,
  tableData,
  paginationParams,
  total,
  initTableList,
  handleSizeChange,
  handleReset,
  handleDelete,
  detailRef,
  currentDetail,
  searchFromRef,
} = useTableList(searchParams, getUserList)
const tableRef = ref()
const multipleSelection = ref([])
const emit = defineEmits(['change'])
const dialogVisible = ref(false)
const isShowOperate = ref(true)
const handleClose = () => {
  multipleSelection.value = []
  tableRef.value && tableRef.value.clearSelection()
  tableData.value = []
  dialogVisible.value = false
}
const handleSubmit = () => {
  emit('change', multipleSelection.value)
  handleClose()
}
watch(
  () => dialogVisible.value,
  () => {
    if (dialogVisible.value) {
      paginationParams.value.pageSize = 9999
      initTableListAndSelect(1)
    }
  },
  {
    immediate: true,
    deep: true,
  },
)
const handleSelectOne = (selection, row) => {
  // 判断是选中/不选中
  const index = selection.findIndex((i) => i.pkid === row.pkid)
  if (index !== -1) {
    multipleSelection.value.push(row)
  } else {
    multipleSelection.value.splice(index, 1)
  }
}
const handleSelectAll = (selection) => {
  // 判断全选/全不选
  if (selection.length > 0) {
    multipleSelection.value = selection
  } else {
    multipleSelection.value = []
  }
}
const initTableListAndSelect = async (pageIndex) => {
  await initTableList(pageIndex)
  nextTick(() => {
    if (multipleSelection.value.length > 0) {
      tableData.value.forEach((row) => {
        const isHas = multipleSelection.value.find((i) => i.pkid === row.pkid)
        isHas && tableRef.value && tableRef.value.toggleRowSelection(row, true)
      })
    }
  })
}
defineExpose({ dialogVisible, multipleSelection, searchParams, isShowOperate })
onMounted(() => {})
</script>
<style lang="scss" scoped>
@import '@/assets/styles/table.scss';
.select {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  width: 100%;
  margin-bottom: 10px;
  span {
    color: #333333;
    font-size: 14px;
    margin-right: 10px;
  }
}
.search {
  padding: 0;
  margin: 0;
  box-shadow: none;
  height: auto;
}
.content {
  padding: 0;
}
.image {
  width: 40px;
  height: 40px;
  margin-right: 10px;
}
</style>
